footer.init();
header.init('店铺设置');

window.onload = function(){

	$('body').on('click','.avatar .shopLogoImg',function(){
		$("input#upload_shop_file").trigger("click");
	});
	
	$('body').on('click','.shop-name',function(){
		$(this).find('input').focus();
	});
	
	$('body').on('blur','.shop-name input',function(){
		var sName = $(this).val();
		if(sName == ''){
			layer.msg('店名不能为空');
			$(this).val('');
			$(this).focus();
			return false;
		}else if( sName.length > 10){
			layer.msg('店名长度不能超过10位');
			$(this).focus();
			return false;
		}
		app.ajax(
			'/Member/Setting/shop_update_data',
			{
				name : sName
			},function(res){
				if(res.code == 200){
					layer.msg('修改店名成功！');
				}else{
					layer.msg(res.msg);
				}
			}
		);
		
	});
	$("body").on("change", "input#upload_shop_file", function(){
		var _this = this;
		var oFile = $(_this)[0].files[0];	
		var rFilter = /^(image\/jpeg|image\/png)$/i;
	    if (! rFilter.test(oFile.type)) {
	        layer.open({content:"文件格式不对",time:2,shadeClose:false});
	        return;
	    }
	    var oReader = new FileReader();
	    oReader.readAsDataURL(oFile);
	    oReader.onload = function(e) {
	    	var result = this.result;
            // 如果图片大于1M，则不能上传
	    	var maxsize = 1024*1024;
            if (e.total > maxsize) {
            	layer.open({content:'图片大小只能1M以下',time:2,shadeClose:false});
            	return false;
            }
            var img = new Image();
            img.src = result;
            if (img.complete) {
                callback();
            } else {
                img.onload = callback;
            }
            function callback() {
                var base64data = compress(img);
                img = null;
                // 上传到服务器
                $.post("http://img.52ywy.com/upload?from=base64&act=userLogo", {imageData:base64data}, function(res){
    	        	if(res.code == 0){
    	        		$.post('/Member/Setting/shop_upload',{logo:res.data[0]},function(d){
    	        			$(".shopLogoImg").attr('src', res.data[0]);
    	        		},"json");
    	        	}else{
    	        		layer.open({content:res.msg,time:2,shadeClose:false});
    	        	}
    	        },"json");
            }
	    };
	});
};

function compress(img) {
	// 用于压缩图片的canvas
	var canvas = document.createElement("canvas");
	var ctx = canvas.getContext('2d');
	// 瓦片canvas
	var tCanvas = document.createElement("canvas");
	var tctx = tCanvas.getContext("2d");
	
	var initSize = img.src.length;
	var width = img.width;
	var height = img.height;
	//如果图片大于四百万像素，计算压缩比并将大小压至400万以下
	var ratio;
	if ((ratio = width * height / 4000000)>1) {
		ratio = Math.sqrt(ratio);
		width /= ratio;
		height /= ratio;
	}else {
		ratio = 1;
	}
	canvas.width = width;
	canvas.height = height;
	// 铺底色
	ctx.fillStyle = "#fff";
	ctx.fillRect(0, 0, canvas.width, canvas.height);
	// 如果图片像素大于100万则使用瓦片绘制
	var count;
	if ((count = width * height / 1000000) > 1) {
		count = ~~(Math.sqrt(count)+1); //计算要分成多少块瓦片
		// 计算每块瓦片的宽和高git 
		var nw = ~~(width / count);
		var nh = ~~(height / count);
		tCanvas.width = nw;
		tCanvas.height = nh;
		for (var i = 0; i < count; i++) {
			for (var j = 0; j < count; j++) {
				tctx.drawImage(img, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh);
				ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh);
    		}
      	}
  	} else {
  		ctx.drawImage(img, 0, 0, width, height);
  	}
  	//进行最小压缩
  	var ndata = canvas.toDataURL('image/jpeg', 0.1);
  	console.log('压缩前：' + initSize);
  	console.log('压缩后：' + ndata.length);
  	console.log('压缩率：' + ~~(100 * (initSize - ndata.length) / initSize) + "%");
  	tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;
  	return ndata;
}

function bytesToSize(bytes) {
    var sizes = ['Bytes', 'KB', 'MB'];
    if (bytes == 0) return 'n/a';
    var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
    return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
};

